package model.searchers.advancedSearchers.nestedLinkedListSearcher;

import model.searchers.URLList;

/**
 * An object of class NestedLinkedList represents a linked list in which each element in the list
 * consists of a string value, another linked list (a URLList), and a pointer to the next element in the list.
 * It is used to maintain a list of words together with a list of which URLs it occurs on.
 *
 */
public class NestedLinkedList extends model.searchers.linkedListSearcher.LinkedList implements Cloneable {

	private URLList url;

	/**
	 * Constructor of NestedLinkedList
	 * 
	 * @param value		string value of the first element in the list
	 * @param url		list of urls on which value occurs
	 * @param next		next element of list
	 */
	public NestedLinkedList(String value, URLList url, NestedLinkedList next) {
		super(value, next);
		this.url = url;
	}

	/**
	 * @return url list of first element of list
	 */
	public URLList getUrl() {
		return url;
	}

	/**
	 * Set the url list of first element to the specified url list
	 * 
	 * @param newUrl url list to be set as new url list
	 */
	public void setUrl(URLList newUrl) {
		this.url = newUrl;
	}

	/**
	 * Create a copy of the list
	 */
	@Override
	public NestedLinkedList clone() {
		NestedLinkedList resultTemp = new NestedLinkedList(this.getValue(),
				this.getUrl().clone(), null);
		NestedLinkedList startOfNewCopy = resultTemp;
		NestedLinkedList copyOfNextElement = null;

		NestedLinkedList next = (NestedLinkedList) this.getNext();
		while (next != null) {
			copyOfNextElement = new NestedLinkedList(next.getValue(), next
					.getUrl().clone(), null);

			resultTemp.setNext(copyOfNextElement);
			resultTemp = (NestedLinkedList) resultTemp.getNext();
			next = (NestedLinkedList) next.getNext();
		}

		return startOfNewCopy;
	}
}
